package com.example.administrator.wallet.view.activity.mine.IncomeRecord;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.ExpenditureRecordBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.administrator.wallet.R.id.tv_currency;
import static com.example.administrator.wallet.R.id.tv_currency_money;
import static com.example.administrator.wallet.R.id.tv_date;
import static com.example.administrator.wallet.R.id.tv_money;

public class IncomeRecordActivity extends AppCompatActivity {

    private ImageButton mIb_back;
    private TextView mV_title;
    private ListView mTransactiob_record;
    private ExpenditureRecordBean expenditureRecordBean;
    private List<ExpenditureRecordBean.DataBean> datas;
    private SimpleAdapter adapter;
    private String language = "0";
    private String page = "1";
    private int code = 0;
    private String type = "3";
    private ExpenditureRecordBean.DataBean dataBean;
    private LinearLayout linear;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_record);

        setStatuColor();


        mIb_back = (ImageButton) findViewById(R.id.finish_img);
        mV_title = (TextView) findViewById(R.id.view_title);
        mTransactiob_record = (ListView) findViewById(R.id.lv_transaction_record);
        linear = (LinearLayout) findViewById(R.id.no_transfer);


        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.TransactionRecords);

        datas=new ArrayList<>();
        final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
        reqequestManager.httpPost(Api.BaseUrl + Api.Currency_Records, Api.getCurrencyRecords(SharedPrefsUtil.getValue(IncomeRecordActivity.this,"wallet", "token",""),language,page,type), new IRequestCallback(){
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                expenditureRecordBean = gson.fromJson(Response, ExpenditureRecordBean.class);
                code = expenditureRecordBean.getCode();
                if (code == 501){
                    linear.setVisibility(View.VISIBLE);
                    mTransactiob_record.setVisibility(View.GONE);
                    Toast.makeText(IncomeRecordActivity.this,R.string.no_data,Toast.LENGTH_SHORT).show();
                }else if (code == 200){
                    linear.setVisibility(View.GONE);
                    mTransactiob_record.setVisibility(View.VISIBLE);
                    datas.addAll(expenditureRecordBean.getData());
                    List<HashMap<String,Object>> list = new ArrayList<HashMap<String, Object>>();
                    for (int i = 0; i < datas.size(); i++) {
                        HashMap<String,Object> map = new HashMap<String,Object>();
                        dataBean = datas.get(i);
                        map.put("currency",dataBean.getCoin_symbol());
                        map.put("currency_money",dataBean.getValue_dec());
                        map.put("date",dataBean.getCreated_at());
                        map.put("money",dataBean.getMoney());
                        list.add(map);
                    }
                    adapter = new SimpleAdapter(IncomeRecordActivity.this,list, R.layout.item_transaction_record,new String[]{"currency","currency_money","date","money"},new int[]{tv_currency,tv_currency_money,tv_date ,tv_money});
                    mTransactiob_record.setAdapter(adapter);
                }else if (code == 401){
                    linear.setVisibility(View.VISIBLE);
                    mTransactiob_record.setVisibility(View.GONE);
                    Toast.makeText(getApplicationContext(),R.string.requestinvalid,Toast.LENGTH_SHORT).show();
                }else if (code == 500){
                    linear.setVisibility(View.VISIBLE);
                    mTransactiob_record.setVisibility(View.GONE);
                    Toast.makeText(getApplicationContext(),code + R.string.Internalerror,Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(String error) {

            }
        });

        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();

    }

    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

}
